import { useMutation, useQueryClient } from '@tanstack/react-query'
import toast from 'react-hot-toast'
import { createEditCabin } from '../../services/apiCabins.js'

export function useCreateCabin() {
	const queryClient = useQueryClient()
	const { mutate: createCabin, isPending: isCreating } = useMutation({
		mutationFn: (newCabinData) => createEditCabin(newCabinData),
		onSuccess: () => {
			queryClient.invalidateQueries({ queryKey: ['cabins'] })
		},
		onError: (err) => {
			toast.error(err.message)
		}
	})

	return { createCabin, isCreating }
}
